/*
 * Copyright 2011 John Alström
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package se.elva.slticket;

import se.elva.slticket.dialog.InfoDialog;
import se.elva.slticket.price.PriceInfo;
import se.elva.slticket.price.PricePlan;
import se.elva.slticket.price.PriceVastmaland;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class VasterasLansTrafik extends SmsTicket {
	/** Called when the activity is first created. */

	private int ticketPrice = 0;
	private PricePlan pricePlan;
	private PriceInfo priceInfo = null;
	private final String FULLPRICE = "V";
	private final String TEEN = "S";
	private final String NIGHT = "AT";

	private String city = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		pricePlan = new PriceVastmaland();
		setContentView(R.layout.vasteraslayout);
		updateTicketPrice();
		((RadioButton) findViewById(R.id.RadioButtonWestVuxen))
				.setOnCheckedChangeListener(priceChangeListener);
		((RadioButton) findViewById(R.id.RadioButtonWestUngdom))
				.setOnCheckedChangeListener(priceChangeListener);
		((RadioButton) findViewById(R.id.RadioButtonArosTian))
				.setOnCheckedChangeListener(priceChangeListener);

		((Button) findViewById(R.id.send)).setOnClickListener(mBackListener);
		((Button) findViewById(R.id.cancel))
				.setOnClickListener(mCancelListener);
		setPickCitySpinner();
		((Spinner) findViewById(R.id.pickCity))
				.setOnItemSelectedListener(mListener);

	}

	private void setPickCitySpinner() {
		Spinner s = (Spinner) findViewById(R.id.pickCity);
		ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this,
				R.array.vasteras, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		s.setAdapter(adapter);

	}

	OnItemSelectedListener mListener = new OnItemSelectedListener() {

		public void onItemSelected(AdapterView<?> arg0, View arg1, int which,
				long arg3) {
			/* User clicked so do some stuff */
			if (which != 0) {
				((RadioButton) findViewById(R.id.RadioButtonArosTian))
						.setVisibility(RadioButton.INVISIBLE);
				((RadioGroup) findViewById(R.id.RadioGroup02))
						.check(R.id.RadioButtonWestVuxen);

			} else {
				((RadioButton) findViewById(R.id.RadioButtonArosTian))
						.setVisibility(RadioButton.VISIBLE);
			}
			if (which == 0) {
				city = "V";
			} else if (which == 1) {
				city = "VK";
			} else if (which == 2) {
				city = "A";
			} else if (which == 3) {
				city = "KP";
			} else if (which == 4) {
				city = "F";
			} else if (which == 5) {
				city = "SA";
			}
			priceInfo = pricePlan.getPrinceInfo(city);
			updateTicketPrice();

		}

		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub

		}
	};

	// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
	// = listeners
	// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

	OnCheckedChangeListener priceChangeListener = new OnCheckedChangeListener() {
		public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
			updateTicketPrice();
		}
	};

	OnClickListener mPickCityListener = new OnClickListener() {
		public void onClick(View v) {
			showDialog(1);
		}
	};

	OnClickListener mBackListener = new OnClickListener() {
		public void onClick(View v) {
			String price = getPrice();
			// "72372"
			if (!"".equals(city) && !"".equals(price)) {
				// Send notifaction to the user that the sms has been send
				saveState(((Spinner) findViewById(R.id.pickCity))
						.getSelectedItemPosition());
				sendSms(getPhoneNumer(), city + price, VasterasLansTrafik.this);
			} else {
				showInfoDialog("Fel", getString(R.string.errorZone));
			}
		}
	};

	private void updateTicketPrice() {
		String priceCategory = getPrice();
		if (priceInfo != null) {
			if (priceCategory.equalsIgnoreCase(FULLPRICE)) {
				ticketPrice = (int) priceInfo.getAdult();
			} else if (priceCategory.equalsIgnoreCase(TEEN)) {
				ticketPrice = (int) priceInfo.getTeen();
			} else if (priceCategory.equalsIgnoreCase(NIGHT)) {
				ticketPrice = (int) priceInfo.getNight();
			}
		}
		((TextView) findViewById(R.id.Summa)).setText(getString(
				R.string.ticketPrice).replace(":ticketPrice",
				String.valueOf(ticketPrice)));
		String code = getString(R.string.smsInformationVasterasLansTradik);
		code = code.replace(":smsCode", String.valueOf(city + priceCategory));
		code = code.replace(":telefon", getPhoneNumer());

		((TextView) findViewById(R.id.code)).setText(code);
	}

	private String getPrice() {
		if (((RadioButton) findViewById(R.id.RadioButtonWestVuxen)).isChecked()) {
			return FULLPRICE;
		} else if (((RadioButton) findViewById(R.id.RadioButtonWestUngdom))
				.isChecked()) {
			return TEEN;
		} else if (((RadioButton) findViewById(R.id.RadioButtonArosTian))
				.isChecked()) {
			return NIGHT;
		}
		return "";
	}

	@Override
	protected void showInfoDialog(String title, String msg) {
		final InfoDialog info = new InfoDialog(VasterasLansTrafik.this, msg);
		info.show();
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		if (id == 0) {
			return new AlertDialog.Builder(VasterasLansTrafik.this)
					.setTitle(R.string.pickCity)
					.setItems(R.array.city,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									saveChoice = which;
									Intent intent = findIntentToStart(
											VasterasLansTrafik.this, which);
									startActivity(intent);
									finish();
								}
							}).create();
		} else {
			return null;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		setCitySpinner(getState());
		SharedPreferences prefs = getSharedPreferences("SLTicket", 0);
		String restoredText = prefs.getString("saveChoice", null);
		if (restoredText != null) {
			saveChoice = Integer.valueOf(restoredText);
		} else {
			showDialog(0);
		}
	}

	private void setCitySpinner(int i) {
		((Spinner) findViewById(R.id.pickCity)).setSelection(i);
	}

	private void saveState(int i) {
		SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
		editor.putInt("latest", i);
		editor.commit();
	}

	private int getState() {
		return getPreferences(MODE_WORLD_READABLE).getInt("latest", 0);
	}

	@Override
	public String getPhoneNumer() {
		return getPrefix() + Constans.PHONENUMBER_Vastmalands;
	}

	@Override
	public String getTextToSend() {
		return city + getPrice();
	}

}
